import hashlib
from _md5 import md5

from flask import Flask, escape, request
import pandas as pd
import dbconnect
import json
import http_rest
from datetime import datetime, timedelta
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.config['SECRET_QUESTION'] = "e031b522814fe10a6731afe304ca22fe"
app.config['SECRET_ANSWER'] = "593512f9e3c0d5cdf6e82a16f635a8be"
unautchrequest = {
'ResponceCode': '-99',
'ResponceText': 'WRONG ANSWER',
}

@app.route('/')
def hello():
    return f'<h1>Welcome to ReffAppApi</h1>'


@app.route('/login', methods=['POST'])
def login():
    # md5(QUESTION+login+password)
    try:
        question = request.form.get("question", "")
        login = request.form.get("login", "")
        password = request.form.get("password", "")
        res = 1
    #responceanswer = str(hashlib.md5((question+login+password).encode('utf-8')).hexdigest())

        if  res == 1:
            return {
                    'ResponceCode': '1',
                    'ResponceText': 'login successful',
                    'ResponceAnswer': app.config['SECRET_ANSWER']
            }
        else:
            return {
                'ResponceCode': '0',
                'ResponceText': 'login fail',
                'ResponceAnswer': ''
            }
    except:
        return {
            'ResponceCode': '0',
            'ResponceText': 'login fail',
            'ResponceAnswer': ''
        }

@app.route('/SearchCont', methods=['POST'])
def SearchCont():
    if request.form.get("responceanswer", "") == app.config['SECRET_ANSWER']:
        number = request.form.get("number",'')
        Cont = http_rest.ContainerInfo()
        temp = Cont.SearchByNum(number)
        return temp
    else:
        return unautchrequest

@app.route('/applyaction', methods=['POST'])
def applyAction():
    if request.form.get("responceanswer", "") == app.config['SECRET_ANSWER']:
        contRefKey = request.form.get("Ref_Key",'')
        userRefKey = request.form.get("User_Ref_Key", '')
        action = request.form.get("action", '')
        temp = request.form.get("temp", '')
        http_rest.cdAction(contRefKey,userRefKey,action,temp)
        return 'ok'
    else:
        return unautchrequest

@app.route('/updaterefjournal', methods=['POST'])
def updaterefjournal():
    if request.form.get("responceanswer", "") == app.config['SECRET_ANSWER']:
        DocNum = request.form.get("DocNum", '')
        Ref_Key = request.form.get("Ref_Key", '')
        Alarm = request.form.get("Alarm", '')
        Fact_Temp = request.form.get("Fact_Temp", '')
        http_rest.updateRefJournal(DocNum,Ref_Key,Alarm,Fact_Temp)
        return "ok"
    else:
        return unautchrequest


@app.route('/getContInfo', methods=['POST'])
def getContInfo():
    if request.form.get("responceanswer", "") == app.config['SECRET_ANSWER']:
        Cont = http_rest.ContainerInfo()
        Cont.RefKey = request.form.get("RefKey", '')
        return  Cont.FillContainerInfo()
    else:
        return unautchrequest


@app.route('/getuserslist', methods=['POST'])
def getuserslist():
    if request.form.get("responceanswer", "") == app.config['SECRET_ANSWER']:
        return http_rest.get_users_list()
    else:
        return  unautchrequest


@app.route('/getlastref', methods=['POST'])
def getlastref():
    if request.form.get("responceanswer", "") == app.config['SECRET_ANSWER']:
        User_Ref_Key = request.form.get("RefKey", '')
        new_Jornal = request.form.get("newJornal", '')
        return http_rest.getlastref(User_Ref_Key,new_Jornal)
    else:
        return  unautchrequest



if __name__ == '__main__':
    app.run('0.0.0.0')